import Vue from 'vue'
// import App from './App.vue'
// import router from './router'
// import store from './store'

import App from './App'
import common from './kits/common.js'
// import router from './router'
import moment from 'moment'
Vue.prototype.$moment = moment
moment.locale('zh-cn')
Vue.config.productionTip = false
import util, { pushNoRepeat, remove } from '@/kits/util'
import areaJudgment from '@/kits/areaJudgment.js'
import filterMethods from '@/kits/filterMethod.js'
import directiveMethods from '@/kits/directiveMethod.js'
import accessControl from '@/kits/accessControl.js'
import sortTableColumn from '@/mixin/sortTableColumn'
//引入字体
// import '../static/HanSans/Bold/Bold.css';
// import '../static/HanSans/Regular/SourceHanSansCN-Regular.css';
import '../static/HanSans/Light/SourceHanSansCN-Light.css'

import htmlToPdf from '@/kits/htmlToPdf.js'
// 使用Vue.use()方法就会调用工具方法中的install方法
Vue.use(htmlToPdf)
Vue.mixin(sortTableColumn)
// 批量注册公共方法
for (let key in util) {
  Object.defineProperty(Vue.prototype, key, {
    value: util[key],
    writable: true,
  })
}
// 批量注册区域判断
for (let key in areaJudgment) {
  Object.defineProperty(Vue.prototype, key, {
    value: areaJudgment[key],
  })
}
// 批量注册filter
for (let key in filterMethods) {
  Vue.filter(key, filterMethods[key])
}

// 批量注册自定义指令
for (let key in directiveMethods) {
  Vue.directive(key, directiveMethods[key])
}

// js数组实现不重复插入数据
Array.prototype.pushNoRepeat = pushNoRepeat

// 删除数组里面的某一个值
Array.prototype.remove = remove

//权限控制公共方法
Vue.prototype.accessControl = accessControl

//注册全局列设置组件
import colSetup from '@/components/publicCom/colSetup.vue'
Vue.component('colSetup', colSetup)

//注册全局分页组件
import Pagination from '@/components/publicCom/pagination.vue'
Vue.component('Pagination', Pagination)

//注册全局分页组件（武汉定制化需求）
import paginationWH from '@/components/publicCom/paginationWH.vue'
Vue.component('paginationWH', paginationWH)

//注册全局标题组件
import topTitle from '@/components/publicCom/topTitle.vue'
Vue.component('topTitle', topTitle)

//注册全局空白页组件
import emptyTable from '@/components/publicCom/emptyTable.vue'
Vue.component('emptyTable', emptyTable)

//注册全局上传附件组件
import uploadAnnex from '@/components/publicCom/uploadAnnex.vue'
Vue.component('uploadAnnex', uploadAnnex)

//注册图片放大和删除公共组件
import elementImage from '@/components/publicCom/elementImage.vue'
Vue.component('elementImage', elementImage)

//支持pdf附件展示组件
import uploadResult from '@/components/publicCom/uploadResult'
Vue.component('uploadResult', uploadResult)

// 附件列表
import contractEnclosure from '@/components/publicCom/contractEnclosure'
Vue.component('contractEnclosure', contractEnclosure)

// 附件列表弹窗
import contractEnclosureDialog from '@/components/publicCom/contractEnclosureDialog'
Vue.component('contractEnclosureDialog', contractEnclosureDialog)

//注册时间选择公共组件
import timeSelection from '@/components/publicCom/timeSelection.vue'
Vue.component('timeSelection', timeSelection)

//复制文本公共组件
import copyText from '@/components/publicCom/copyText.vue'
Vue.component('copyText', copyText)

//表格，操作列显示更多
import showMore from '@/components/publicCom/showMore.vue'
Vue.component('showMore', showMore)

//注册导出按钮
import exportBtn from '@/components/publicCom/exportBtn.vue'
Vue.component('exportBtn', exportBtn)

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

import router from './router'
import axios from 'axios'
Vue.prototype.axios = axios
axios.defaults.withCredentials = true

import 'babel-polyfill'

//引入qs
import qs from 'qs'
Vue.prototype.$qs = qs

//设置请求数据的ip,引入公共js
Vue.use(common)
Vue.prototype.common = common
axios.defaults.baseURL = common.commonApi

// 导入一个当前系统的全局基本样式
import './../static/css/reset.css'
// import '!style-loader!css-loader!less-loader!@/assets/css/reset.less';

import store from './store'

// animate
import animated from 'animate.css'
Vue.use(animated)

import echarts from 'echarts'
Vue.prototype.$echarts = echarts
// import '../static/js/echarts-liquidfill/echarts-liquidfill.js';

//引入字体图标
import './assets/iconfont/iconfont.css'
// import './assets/menuIcon/iconfont.css';

// axios兼容
import promise from 'es6-promise'
promise.polyfill()

//富文本编译器ueditor
import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min'

import 'jquery'
import 'jquery.nicescroll'
import './kits/jquery.form'

require('!style-loader!css-loader!less-loader!./assets/color.less') //引入换肤css
//复制粘贴功能
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
// http request 拦截器
axios.interceptors.request.use(
  (config) => {
    if (config.method === 'post' || config.method === 'put') {
      if (config.data) {
        if (!(config.headers['Content-Type'] === 'application/json')) {
          config.data = qs.stringify(config.data)
        }
      } else {
        config.data = qs.stringify(config.data)
      }
    }

    let token = localStorage.getItem('access_token')
    if (token) {
      if (config.url.indexOf('api-out/qtzbxmxx/list') == -1) {
        config.headers.Authorization = 'Bearer ' + token
      }
      // let deviceId = localStorage.getItem("APP_ID");
      // deviceId && (config.headers['x-self-deviceId'] = deviceId);
    }
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

import { Message } from 'element-ui'

//http response 拦截器
axios.interceptors.response.use(
  (response) => {
    return response //请求成功的时候返回的data
  },
  (error) => {
    try {
      if (error.response) {
        switch (error.response.status) {
          case 401: //token过期，清除它,清除token信息并跳转到登录页面
            localStorage.setItem('access_token', '')
            localStorage.setItem('refresh_token', '')
            localStorage.setItem('userName', '')
            // 只有在当前路由不是登录页面才跳转
            if (router.currentRoute.path !== '/login/loginFirst') {
              router.replace({
                path: '/login/loginFirst',
              })
            }
            //清除tab栏数据
            this.$store.state.sliderBar = []
            localStorage.removeItem('sliderBar')
            return
          case 403: //权限
            //无权限跳转到403页面
            if (router.currentRoute.path !== '/home/emptyPage/403') {
              router.replace({
                path: '/home/emptyPage/403',
              })
            }
            return
          case 418: //SQL注入
            //参数中含有非法字符，请重新输入。
            Message({
              type: 'warning',
              message: '参数中含有非法字符，请重新输入。',
              duration: 1500,
            })
        }
      }
    } catch (e) {}
  }
)

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app')
